package com.clinbrain;

import com.facebook.presto.spi.function.Description;
import com.facebook.presto.spi.function.ScalarFunction;
import com.facebook.presto.spi.function.SqlNullable;
import com.facebook.presto.spi.function.SqlType;
import com.facebook.presto.spi.type.StandardTypes;
import io.airlift.slice.Slice;
import io.airlift.slice.Slices;

import java.util.Objects;

/**
 * @author : agui
 * @version V1.0
 * @Project: presto-udf
 * @Package： com.clinbrain
 * @Description: TODO
 * @date Date : 2023年06月30日 10:53
 */
public final class PatientInfectiousDisease {

    private static final String UNKNOWN = "?";

    private PatientInfectiousDisease() {

    }

    @ScalarFunction("patient_infectious_disease")
    @Description("convert cache date to standard date format.")
    @SqlType(StandardTypes.VARCHAR)
    @SqlNullable
    public static Slice patient_infectious_disease(@SqlType(StandardTypes.VARCHAR) Slice hbvStatusSlice,
                                                   @SqlType(StandardTypes.VARCHAR) Slice hcvStatusSlice,
                                                   @SqlType(StandardTypes.VARCHAR) Slice hivStatusSlice,
                                                   @SqlType(StandardTypes.VARCHAR) Slice syphilisStatusSlice) {
        String hbvStatus = Objects.isNull(hbvStatusSlice) ? UNKNOWN : hbvStatusSlice.toStringUtf8();
        String hcvStatus = Objects.isNull(hcvStatusSlice) ? UNKNOWN : hcvStatusSlice.toStringUtf8();
        String hivStatus = Objects.isNull(hivStatusSlice) ? UNKNOWN : hivStatusSlice.toStringUtf8();
        String syphilisStatus = Objects.isNull(syphilisStatusSlice) ? UNKNOWN : syphilisStatusSlice.toStringUtf8();

        String content = String.format("乙肝[%s]丙肝[%s]艾滋[%s]梅毒[%s]", hbvStatus, hcvStatus, hivStatus, syphilisStatus);
        if (content.contains("未检")) {
            return Slices.utf8Slice(UNKNOWN);
        }

        String replace = content.replaceAll("阳性", "+")
                .replaceAll("阴性", "-")
                .replace("乙肝[-]", "")
                .replace("丙肝[-]", "")
                .replace("艾滋[-]", "")
                .replace("梅毒[-]", "")
                .replace("乙肝[+]", "乙")
                .replace("丙肝[+]", "丙")
                .replace("艾滋[+]", "HIV")
                .replace("梅毒[+]", "T")
                .replace("乙肝[?]丙肝[?]艾滋[?]梅毒[?]", UNKNOWN);

        return Slices.utf8Slice(replace.contains(UNKNOWN) ? UNKNOWN : replace);
    }
}
